package cn.gov.pbc.gd.credit.provider.api.corp.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.service.db.impl.DBBaseServiceImpl;
import com.liu.tools.PrimaryUtil;

import cn.gov.pbc.gd.credit.api.corp.ICorpApi;
import cn.gov.pbc.gd.credit.provider.dao.corp.ICorpDao;
import cn.gov.pbc.gd.credit.provider.dao.role.IRoleDao;
import cn.gov.pbc.gd.credit.provider.dao.userrole.IUserRoleDao;
import cn.gov.pbc.gd.credit.vo.corp.CorpVo;
import cn.gov.pbc.gd.credit.vo.corp.ResCorpVo;
import cn.gov.pbc.gd.credit.vo.userrole.UserRoleVo;

/**
 * @datatime 2017年10月16日 下午2:50:56
 * @author liuxingmi
 * @desc
 */
@Service
public class CorpApiImpl extends DBBaseServiceImpl<ResCorpVo, CorpVo, ICorpDao> implements ICorpApi{

	
	
	@Autowired
	ICorpDao corpDao;
	@Autowired
	IUserRoleDao userRoleDao;
	@Autowired
	IRoleDao roleDao;
	@Override
	public int insert(CorpVo record) {
		
		int t=corpDao.insert(record);
		if(t==1){
			List<String> list = record.getIdList();
			UserRoleVo userRoleVo;
			if(list != null && list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					userRoleVo=new UserRoleVo();
					userRoleVo.setId(PrimaryUtil.primaryId("R"));
					userRoleVo.setRoleId(list.get(i));
					userRoleVo.setUserType(2);
					userRoleVo.setCorpUserId(record.getId());
					userRoleDao.insert(userRoleVo);
				}
			}
			
			
		}else{
			return 0;
		}
		return 1;
	}
	
	@Override
	public int update(CorpVo record) {
		
		int t=corpDao.update(record);
		if(t==1){
			List<String> list=record.getIdList();
			UserRoleVo ur=new UserRoleVo();
			ur.setCorpUserId(record.getId());
			userRoleDao.delete(ur);
			UserRoleVo userRoleVo;
			if(list!=null&&list.size()>0){
				for (int i = 0; i < list.size(); i++) {
					userRoleVo=new UserRoleVo();
					userRoleVo.setId(PrimaryUtil.primaryId("R"));
					userRoleVo.setRoleId(list.get(i));
					userRoleVo.setUserType(2);
					userRoleVo.setCorpUserId(record.getId());
					userRoleDao.insert(userRoleVo);
				}
			}
			return 1;
		}else{
			return 0;
		}
		
	}


	@Override
	public List<ResCorpVo> listById(String id) {
		
		return corpDao.listById(id);
	}


	@Override
	public List<ResCorpVo> treeCropList(CorpVo vo) {
		return corpDao.treeCropList(vo);
	}
}
